package cn.airfei.aircore.testcore.service.impl;

import cn.airfei.aircore.testcore.domain.convertor.GoodsConvertor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.airfei.aircore.testcore.services.service.impl.GoodsDomainServiceImpl;
import cn.airfei.aircore.testcore.services.entity.Goods;
import cn.airfei.aircore.testcore.storage.dto.GoodsDto;
import cn.airfei.aircore.testcore.storage.vo.GoodsVo;
import cn.airfei.aircore.testcore.service.IGoodsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import cn.airfei.aircore.core.storage.PageParam;
import cn.airfei.aircore.core.enums.RespEnum;
import cn.airfei.aircore.core.exception.AppException;
import cn.airfei.aircore.core.utils.QueryParamUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 商品
 * </p>
 *
 * @author air
 * @since 2022-04-24
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Resource
    private GoodsConvertor goodsConvertor;

    @Resource
    private GoodsDomainServiceImpl goodsDomainService;

    /**
     *
     * @param goodsDto
     * @param pageParam
     * @return
     */
    @Override
    public Page<GoodsVo> index(GoodsDto goodsDto,PageParam pageParam) {
        return goodsDomainService.index(goodsDto,pageParam);
    }

    /**
     * 分页查询单表数据
     *
     * @param map
     * @return
     */
    @Override
    public Page<GoodsVo> singleTablePage(Map<String, Object> map) {
        Page<Goods> goodsPage=goodsDomainService.singleTablePage(map);
        List<GoodsVo> goodsVoList = goodsConvertor.entityList2VoList(goodsPage.getRecords());
        Page goodsVoPage = goodsPage;
        goodsVoPage.setRecords(goodsVoList);
        return goodsVoPage;
    }

    /**
     * 显示单条数据
     *
     * @param id
     * @return
     */
    @Override
    public GoodsVo show(String id){
        Goods goods = goodsDomainService.show(id);
        return goodsConvertor.entity2Vo(goods);
    }

    /**
     * 删除（销毁表数据）
     *
     * @param ids
     * @return
     */
    @Override
    public Integer destroy(String ids){
        return goodsDomainService.destroy(ids);
    }

    /**
     * 更新表数据
     * @param goodsDto
     * @return
     */
    @Override
    public Integer update(GoodsDto goodsDto){
        Integer res = goodsDomainService.update(goodsConvertor.dto2Entity(goodsDto));
        if (res < 1) {
            throw new AppException(RespEnum.UPDATE_FAIL.getCode(), RespEnum.UPDATE_FAIL.getMsg());
        }
        return res;
    }

    /**
     * 保存表数据
     *
     * @param {entity?uncap_first}Dto
     * @return
     */
    @Override
    public Integer store(GoodsDto goodsDto){
        return goodsDomainService.store(goodsConvertor.dto2Entity(goodsDto));
    }

    /**
     * 设置状态，1:正常,2:禁用/待审核，-1:已删除
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean setStatus(Integer id, Integer status) {
        if(goodsDomainService.setStatus(id,status)>0){
            return true;
        }
        throw new AppException(RespEnum.SET_STATUS_ERROR.getCode(),RespEnum.SET_STATUS_ERROR.getMsg());
    }

    /**
     * get all resource in storage
     *
     * @return
     */
    @Override
    public List<GoodsVo> getList(Map<String,Object> map) {
        QueryWrapper<Goods> queryWrapper = QueryParamUtil.getEntityWrapper(map, Goods.class);
        return goodsConvertor.entityList2VoList(goodsDomainService.list(queryWrapper));
    }

    /**
     * 更新多条数据（主键id必传）
     *
     * @return
     */
    @Override
    public List<GoodsVo> updateBatch(List<GoodsDto> goodsDtoList){
        List<Goods> goodsList = goodsConvertor.dtoList2EntityList(goodsDtoList);
        goodsDomainService.updateBatchById(goodsList);
        return goodsConvertor.entityList2VoList(goodsList);
    }

     /**
      * 添加多条数据
      *
      * @return
      */
    @Override
    public List<GoodsVo> saveBatch(List<GoodsDto> goodsDtoList){
        List<Goods> goodsList = goodsConvertor.dtoList2EntityList(goodsDtoList);
        goodsDomainService.saveBatch(goodsList);
        return goodsConvertor.entityList2VoList(goodsList);
    }


}
